package com.yuma.rabbitmq.work.main;

import com.rabbitmq.client.*;
import com.yuma.rabbitmq.work.util.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer1 {

    private static final String QUEUE_NAME = "work_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("Consumer1: 收到的消息" + new String(body));
            }
        };

        // 如果消费者要监听的队列不存在则会抛出IO异常
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}
